using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace SimplIDE
{
  public partial class NewProjectForm : Form
  {
    FileTree tmp;

    public NewProjectForm(FileTree tr)
    {
      InitializeComponent();
      txtBoxProject.Text = SuggestName();
      DirectoryInfo di = new DirectoryInfo(@".\");
      txtBoxDir.Text = di.FullName + txtBoxProject.Text;
      tmp = tr;
    }

    private string SuggestName()
    {
      string name = "NewProject";
      int counter = 2;
      while (Directory.Exists(name))
        name = "NewProject" + (counter++).ToString();
      return name;
    }

    private void buttonOk_Click(object sender, EventArgs e)
    {
      if (Create())
      {
        tmp.InitFileTree(txtBoxDir.Text, txtBoxProject.Text);
        this.Close();
      }
    }

    private void buttonCancel_Click(object sender, EventArgs e)
    {
      this.Close();
    }
    
    private string GetDefaultMainCPP()
    {
      string endl = Environment.NewLine;
      string s = string.Empty;
      s += endl;
      s += "#include <stdio.h>" + endl;
      s += endl;
      s += "/*" + endl;
      s += " * external entry point" + endl;
      s += " */" + endl;
      s += "int main(int argc, char** argv)" + endl;
      s += "{" + endl;
      s += "  // put your main code here, for example:" + endl;
      s += "  // printf(\"Hello, World!\");" + endl;
      s += "  return 0;" + endl;
      s += "}" + endl;
      return s;
    }

    private bool Create()
    {
      if (!Directory.Exists(txtBoxDir.Text))
      {
        string path = txtBoxDir.Text + (txtBoxDir.Text.EndsWith("\\") ? "" : "\\");
        Directory.CreateDirectory(path);
        string endl = Environment.NewLine;

        File.WriteAllText(path + "buildoptions.txt", "");
        File.WriteAllText(path + "runoptions.txt", "");
        File.WriteAllText(path + "main.cpp", GetDefaultMainCPP());
        File.WriteAllText(path + txtBoxProject.Text + ".smp", txtBoxDir.Text + endl + txtBoxProject.Text + endl);

        XmlDocument xdoc;
        XmlNode xnode;
        XmlElement xelem;
        XmlElement childxelem;

        xdoc = new XmlDocument();
        xnode = xdoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
        xdoc.AppendChild(xnode);
        xelem = xdoc.CreateElement("", "Directory", "");
        xelem.SetAttribute("path", txtBoxDir.Text);
        xdoc.AppendChild(xelem);
        xelem = xdoc.CreateElement("", "Project", "");
        xelem.SetAttribute("name", txtBoxProject.Text);
        xdoc.ChildNodes.Item(1).AppendChild(xelem);
        childxelem = xdoc.CreateElement("", "File", "");
        childxelem.SetAttribute("path", txtBoxDir.Text + @"\main.cpp");
        childxelem.SetAttribute("name", "main.cpp");
        xelem.AppendChild(childxelem);

        try
        {
          xdoc.Save(path + "ProjectConfig.xml");
        }
        catch (Exception e)
        {
          MessageBox.Show(e.Message);
        }
        return true;
      }
      else
      {
        MessageBox.Show("Directory already exists!");
        return false;
      }


    }
  }
}